package eleven.senven.hpb.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import eleven.senven.hpb.R;
import eleven.senven.hpb.adapter.SecondGridAdapter;
import eleven.senven.hpb.bean.SecondBean;
import eleven.senven.hpb.ui.SecondDetail;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommunityFragment extends Fragment {
    private String Second_url="http://api.meishixing.com/other/top/first/city_id=3";
   //@ViewById(R.id.hot_back)
   private ImageView back;
    //@ViewById(R.id.hot_grid)
    private GridView gv;
    private List<Map<String, Object>> gvList;
    public CommunityFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_community, container, false);
        initView(view);
        loadData();
        return view;
    }

    private void loadData() {
        gvList=new ArrayList<>();
        HttpUtils httpUtils=new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET,Second_url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                SecondBean bean= JSONObject.parseObject(responseInfo.result,SecondBean.class);
                final List<SecondBean.ResultEntity> result=bean.getResult();
                gv.setAdapter(new SecondGridAdapter(result,getActivity(),new BitmapUtils(getActivity())));
                gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getActivity(), SecondDetail.class);
                        intent.putExtra("name",result.get(position).getType_desc());
                        startActivity(intent);

                    }
                });


            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });


    }

    private void initView(View view) {
        back=(ImageView)view.findViewById(R.id.hot_back);
        gv=(GridView)view.findViewById(R.id.hot_grid);



    }


}
